package com.aspectlog.aspectlogspringbootstarter.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.PriorityOrdered;

@Aspect
@EnableAspectJAutoProxy
@Configuration
@ConditionalOnProperty(prefix = "aspectLog", name = "enable", havingValue = "true", matchIfMissing = true)
public class AspectLogAop implements PriorityOrdered {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Around("@annotation(com.aspectlog.aspectlogspringbootstarter.log.AspectLog)")
    public Object isOpen(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        String aa = proceedingJoinPoint.getSignature().toString().substring(proceedingJoinPoint.getSignature().toString().indexOf(" "), proceedingJoinPoint.getSignature().toString().indexOf("("));
        long start = System.currentTimeMillis();
        Object obj = proceedingJoinPoint.proceed();
        long end = System.currentTimeMillis();
        logger.info("aspectlogspringbootstarter:method:{} run:{} ms", aa, (end - start) / 1000);
        return obj;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
